package task.snake.view.swingimpl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import task.snake.controller.StartListener;
import task.snake.controller.StopListener;
import task.snake.controller.TurnListener;
import task.snake.model.SnakeModel;

/**
 * Class SnakeWindow extends class JFrame and implements interface Observer.
 * This class is part of view in MVC model.
 * <p>
 * The class provide constructor and methods for setting listeners.
 * 
 * @author Nadejda Makarkina
 * 
 */
@SuppressWarnings("serial")
public class SnakeWindow extends JFrame implements Observer {
	private JLabel countLabel;
	private PlayPanel playPanel;
	private JButton startBtn;
	private JButton stopBtn;

	/**
	 * Initializes a newly created SnakeWindow.
	 * 
	 * @param m
	 *            number of columns in play grid
	 * @param n
	 *            number of lines in play grid
	 */
	public SnakeWindow(int m, int n) {
		// window
		super("Snake");		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		int panelsWidth = PlayPanel.getCellSize() * m
				+ PlayPanel.getCellPadding() * (m + 1);
		int panelsHeight = PlayPanel.getCellSize() * n
				+ PlayPanel.getCellPadding() * (n + 1);
		// panel for count
		JPanel counterPanel = new JPanel();
		counterPanel.setBackground(Color.GRAY);
		countLabel = new JLabel("000", JLabel.CENTER);
		counterPanel.add(countLabel);
		counterPanel.setPreferredSize(new Dimension(panelsWidth, 30));
		getContentPane().add(counterPanel);
		// play field
		playPanel = new PlayPanel(m, n);		
		playPanel.setPreferredSize(new Dimension(panelsWidth, panelsHeight));
		// panel for buttons
		JPanel buttinsPanel = new JPanel();
		startBtn = new JButton("Start");
		startBtn.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				stopBtn.setEnabled(true);
				startBtn.setEnabled(false);
			}
		});
		stopBtn = new JButton("Stop");
		stopBtn.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				stopBtn.setEnabled(false);
			}
		});
		stopBtn.setEnabled(false);
		buttinsPanel.setPreferredSize(new Dimension(panelsWidth, 35));
		buttinsPanel.add(startBtn);
		buttinsPanel.add(stopBtn);
		buttinsPanel.setBackground(Color.GRAY);
		// main panel
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
		mainPanel.setBackground(Color.LIGHT_GRAY);
		mainPanel.add(counterPanel);
		mainPanel.add(playPanel);
		mainPanel.add(buttinsPanel);
		setContentPane(mainPanel);
		// put window in the center of screen
		pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) (screenSize.width / 2 - getSize().width / 2);
		int y = (int) (screenSize.height / 2 - getSize().height / 2);
		setLocation(x, y);
		setVisible(true);
	}

	/**
	 * Sets ActionListener to a Start button.
	 * 
	 * @param sl
	 */
	public void setStartListener(StartListener sl) {
		startBtn.addActionListener(sl);
	}

	/**
	 * Sets ActionListener to a Stop button.
	 * 
	 * @param sl
	 */
	public void setStopListener(StopListener sl) {
		stopBtn.addActionListener(sl);
	}

	/**
	 * Sets MouseListener to a SnakeWindow.
	 * 
	 * @param tl
	 */
	public void setTurnListener(TurnListener tl) {
		addMouseListener(tl);
	}

	public void update(Observable o, Object arg) {
			SnakeModel sm = (SnakeModel) o;			
			playPanel.setSnakeModel(sm);
			countLabel.setText(String.valueOf(sm.getCounter()));
			playPanel.paintComponent(playPanel.getGraphics());
	}
}
